package com.example.pixelyear.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.pixelyear.Interface.MainViewPageClickListener
import com.example.pixelyear.Interface.OnPixelClickListener
import com.example.pixelyear.R
import com.example.pixelyear.bean.DateBean
import com.example.pixelyear.bean.YearBean
import com.example.pixelyear.utils.Utils
import com.example.pixelyear.utils.extension.verticalManager
import kotlinx.android.synthetic.main.item_main_page.view.*
import java.util.*
import kotlin.Comparator

class MainViewPage2Adapter(val context: Context) : BaseAdapter<YearBean>(mutableListOf()) {
    private var click: MainViewPageClickListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return BaseViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_main_page, parent, false)
        )
    }

    override fun onBindView(itemView: View, bean: YearBean, position: Int) {
        itemView.Item_PixelView.updateMonthAndYear(bean.month, bean.year)
        itemView.Item_PixelView.updateDateList(bean.list)
        itemView.Item_RecyclerMoodLine.verticalManager()
        itemView.Item_RecyclerMoodLine.adapter = MoodLineAdapter(context).also {
            it.updateList(bean.list)
            it.setOnItemClickListener(object : OnItemClick<DateBean> {
                override fun click(bean: DateBean, p: Int) {
                    click?.onClick(
                        bean.year,
                        bean.month,
                        bean.day,
                        bean.color,
                        bean.note.isNotEmpty(),
                        position,
                        bean.mood
                    )
                }
            })
        }

        itemView.Item_PixelView.setOnPixelClickListener(object : OnPixelClickListener {
            override fun OnClick(year: Int, month: Int, day: Int, chooseColor: Int, hasNote: Boolean, mood: String?) {
                click?.onClick(year, month, day, chooseColor, hasNote, position, mood)
            }

            override fun OnLongClick(
                year: Int,
                month: Int,
                day: Int,
                chooseColor: Int,
                hasNote: Boolean,
                mood: String?
            ) {
                click?.onLongClick(year, month, day, chooseColor, hasNote, position, mood)
            }
        })
    }

    //刷新单个页面的颜色
    override fun onItemChangeBindView(
        itemView: View,
        bean: YearBean,
        position: Int,
        payload: String
    ) {
        itemView.Item_PixelView.requestLayout()
    }

    override fun updateItem(position: Int, date: YearBean?, payload: String) {
        super.updateItem(position, date.also {
            it?.list?.sortBy { it.day }
        }, payload)
    }

    fun setOnListener(click: MainViewPageClickListener) {
        this.click = click
    }

}